package config

import (
	"log/slog"
	"os"
	"sync"

	"gopkg.in/yaml.v3"
)

type GlobalConfig struct {
	LogConfig        LogConfig        `yaml:"log_config"`
	ServiceConfig    ServiceConfig    `yaml:"service_config"`
	GinWorkMode      string           `yaml:"gin_work_mode"`
	GinWorkAddr      string           `yaml:"gin_server_addr"`
	WechatInfo       WechatInfo       `yaml:"wechat_info"`
	MainTopic        string           `yaml:"main_topic"`
	ImageConfig      ImageConfig      `yaml:"image_config"`
	FileSaveConfig   FileSaveConfig   `yaml:"file_save_config"`
	RedisUseConfig   RedisUseConfig   `yaml:"redis_use_config"`
	Ingfire          Ingfire          `yaml:"ingfire"`
	WechatUserConfig WechatUserConfig `yaml:"wechat_user_config"`
}

type WechatUserConfig struct {
	DbType string `yaml:"db_type"`
	DbUrl  string `yaml:"db_url"`
	DbName string `yaml:"db_name"`

	WxUserDbConllection string `yaml:"we_user_db_conllection"`
	AlbumDbCollection   string `yaml:"album_db_collection"`
	PhotoDbCollection   string `yaml:"photo_db_collection"`

	// session 的主要标记
	RedisSessionMark       string `yaml:"redis_session_mark"`
	RedisSessionExpiretime int    `yaml:"redis_session_expiretime"`

	// 保存在redis中的key标记
	RedisSessionKeyMark string `yaml:"redis_session_key_mark"`
	RedisOpenidMark     string `yaml:"redis_openid_mark"`
	RedisUseridMark     string `yaml:"redis_userid_mark"`
	RedisUsernameMark   string `yaml:"redis_username_mark"`

	// 保存在cookie 的session标记
	RouteSession string `yaml:"route_session"`
}

type Ingfire struct {
	Keys Keys `yaml:"keys"`
}

type Keys struct {
	SessionKey string `yaml:"session_key"`
	AesKey     string `yaml:"aes_key"`
}

type FileSaveConfig struct {
	ImageConfig ImageConfig `yaml:"image_config"`
}

type ImageConfig struct {
	ImageSavePath          string `yaml:"image_save_path"`
	OptimizedImageSavePath string `yaml:"optimized_image_save_path"`
	RedisKey               string `yaml:"redis_key"`
	RedisExpireTime        int    `yaml:"redis_expire_time"`
}

type WechatInfo struct {
	AppID        string `yaml:"appID"`
	AppSecret    string `yaml:"secret"`
	AppGranttype string `yaml:"grant_type"`
}

type LogConfig struct {
	LogPath LogPaht `yaml:"log_path"`
}

type LogPaht struct {
	DatabaseLogPath string `yaml:"database_log_path"`
	RedisLogPath    string `yaml:"redis_log_path"`
	MainLogPath     string `yaml:"main_log_path"`
}

type ServiceConfig struct {
	RedisConfig    RedisConfig    `yaml:"redis_config"`
	WebpConfig     WebpConfig     `yaml:"webp_config"`
	DatabaseConfig DatabaseConfig `yaml:"database_config"`
}

type DatabaseConfig struct {
	MariadbAddr      string `yaml:"mariadb_addr"`
	DatabasePassword string `yaml:"database_password"`
	DatabaseName     string `yaml:"database_name"`
	DatabaseUserName string `yaml:"database_username"`
	DatabaseCharset  string `yaml:"database_charset"`
}

type RedisConfig struct {
	RedisAddr     string `yaml:"redis_addr"`
	RedisPassword string `yaml:"redis_password"`
	RedisDB       int    `yaml:"redis_db"`
	RedisDBName   string `yaml:"redis_db_name"`
	SaveOpt       string `yaml:"save_opt"`
	SaveWay       string `yaml:"save_way"`
}

type WebpConfig struct {
	WebpAddr string `yaml:"webpsh_adrr"`
}

type RedisUseConfig struct {
	Staticfile                  string `yaml:"staticfile"`
	StaticfileExpiretime        int    `yaml:"staticfile_expiretime"`
	WechatUserSession           string `yaml:"wechat_user_session"`
	WechatUserSessionExpiretime int    `yaml:"wechat_user_session_expiretime"`
}

var (
	dimlightConfig GlobalConfig
	once           sync.Once
)

func GetConfig() *GlobalConfig {
	once.Do(func() {
		// yml文件的读取，将配置配置到文件夹中
		ymlSource, err := os.ReadFile("/root/code/src/photowall/pkg/config/global_config.yaml")
		if err != nil {
			slog.Error("配置yml文件读取失败: " + err.Error())
			panic(err)
		}

		err = yaml.Unmarshal(ymlSource, &dimlightConfig)
		if err != nil {
			slog.Error("yml结构体映射到struct失败: " + err.Error())
			panic(err)
		}
	})

	return &dimlightConfig
}
